package cloud.demarcia.yzcrawler.processors;

import cloud.demarcia.yzcrawler.entities.University;
import cloud.demarcia.yzcrawler.entities.WyEntity;
import cloud.demarcia.yzcrawler.respositories.InformationRepository;
import cloud.demarcia.yzcrawler.utils.Browser;
import cloud.demarcia.yzcrawler.utils.UrlGenerator;
import lombok.extern.slf4j.Slf4j;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.LinkedList;
import java.util.List;

@Component
@Slf4j
public class MajorListProcessor {

    private final UrlGenerator urlGenerator;
    private final InfoProcessor infoProcessor;
    private final InformationRepository informationRepository;

    private Document doc;
    private University university;

    @Autowired
    public MajorListProcessor(UrlGenerator urlGenerator, InfoProcessor infoProcessor, InformationRepository informationRepository) {
        this.urlGenerator = urlGenerator;
        this.infoProcessor = infoProcessor;
        this.informationRepository = informationRepository;
    }

    public void process(University university) {
        List<WyEntity> resultList = new LinkedList<>();
        this.university = university;
        getDocument();
        log.info("Processing " + university + " in profile " + university.getProfile() + "...");
        List<Element> majorElements = getMajorList();
        for (Element majorElement : majorElements) {
            String nextUrl = getNextUrl(majorElement);
            List<WyEntity> info1List = infoProcessor.process(nextUrl, university);
            String univName = university.getUniversityName();
            WyEntity info2 = fillInfo(majorElement);
            for (WyEntity info1 : info1List) {
                info1.setUniversity(univName);
                info1.setMajor(info2.getMajor());
                info1.setResearchOrientation(info2.getResearchOrientation());
                info1.setLearningStyle(info2.getLearningStyle());
                info1.setFacultyAdviser(info2.getFacultyAdviser());
                info1.setProposedEnrollment(info2.getProposedEnrollment());
                info1.setTuimian(info2.getTuimian());
                info1.setCategory(university.getProfile().getName());
                resultList.add(info1);
            }
        }
        log.info("Found " + resultList.size() + " items in " + university + ".");
        informationRepository.saveAll(resultList);
    }

    public void getDocument() {
        String listUrl = urlGenerator.getListUrl(university);
        Browser browser = new Browser();
        doc = browser.browse(listUrl);
        browser.close();
    }

    private List<Element> getMajorList() {
        Elements elements = null;
        try {
            Element element = doc.getElementsByClass("ch-table more-content").get(0);
            elements = element.child(1).children();
        } catch (Exception e) {
            log.warn("无法获取专业列表");
        }
        return elements;
    }

    private String getNextUrl(Element element) {
        String s = element.child(7).child(0).attr("href");
        return "https://yz.chsi.com.cn/" + s;
    }

    private WyEntity fillInfo(Element element) {
        WyEntity info1 = new WyEntity();
        try {
            info1.setMajor(element.child(2).text());
            info1.setResearchOrientation(element.child(3).text());
            info1.setLearningStyle(element.child(4).text());
            info1.setFacultyAdviser(element.child(5).child(0).child(0).text());
            info1.setProposedEnrollment(element.child(6).child(1).attr("data-title"));
            try {
                info1.setTuimian(element.child(8).child(1).attr("data-title"));
            } catch (Exception e) {
                log.info("没有推免信息.");
            }
        } catch (Exception e) {
            log.warn("获取信息失败，可能是网页变动原因导致.");
        }
        return info1;
    }

}
